package servlets;

import java.io.IOException;

import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import compsup.CompanySup;
import compsup.CompanySupHome;

import extras.Md5Encryption;

import markerSession.MarkerSession;
import student.Student;
import student.StudentHome;

public class markerPage_handler extends HttpServlet{


	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)
	throws IOException, ServletException
	{
		Context initialContext = null;
		try {
			initialContext = new InitialContext() ;
		} catch (NamingException e) {
			e.printStackTrace();
		}
		HttpSession session = req.getSession(true);
		MarkerSession marker = (MarkerSession)session.getAttribute("marker");	

		String page = req.getParameter("page") ;
		RequestDispatcher dispatcher = null ;
		if(marker == null){
			dispatcher = req.getRequestDispatcher("index.jsp") ;
			dispatcher .forward(req, res) ;
			return;
		}
		if (page.equalsIgnoreCase("index")) {
			session.removeAttribute("marker");
			dispatcher = req.getRequestDispatcher("index.jsp") ;

		}else if(page.equalsIgnoreCase("markerView")){
			req.setAttribute("listStudents", marker.getStudentListDescriptor());
			req.setAttribute("userName", marker.getFirstname());
			dispatcher = req.getRequestDispatcher("marker/markerView.jsp") ;
		}else if(page.equalsIgnoreCase("markerViewFeedback")){
			InitialContext context = null;
			StudentHome studenthome = null;
			CompanySupHome companySupHome = null;
			Student student = null;
			CompanySup companySup = null;
			try {
				context = new InitialContext();
				studenthome = (StudentHome)context.lookup("MyStudent_L");
				companySupHome = (CompanySupHome)context.lookup("MyCompanySup_L");
			} catch (NamingException e) {
				e.printStackTrace();
			}
			int userId = Integer.parseInt(req.getParameter("userId")) ;
			try {
				student = studenthome.findByPrimaryKey(userId);
				//companySup = companySupHome.findByPrimaryKey(student.getCompanySupFeedback().getAuthor().getId());
			} catch (FinderException e) {
				e.printStackTrace();
			}
			req.setAttribute("student", student.getDescriptor());
			req.setAttribute("feedback", student.getCompanySupFeedback().getDescriptor());
			//req.setAttribute("companySup", companySup.toCompanySupDescriptor());
			//req.setAttribute("company", companySup.getCompany().getDescriptor());
			dispatcher = req.getRequestDispatcher("marker/markerViewFeedback.jsp") ;

		}else if(page.equalsIgnoreCase("markerViewReport")){
			InitialContext context = null;
			StudentHome studenthome = null;
			Student student = null;
			try {
				context = new InitialContext();
				studenthome = (StudentHome)context.lookup("MyStudent_L");
			} catch (NamingException e) {
				e.printStackTrace();
			}
			int userId = Integer.parseInt(req.getParameter("userId")) ;
			try {
				student = studenthome.findByPrimaryKey(userId);
			} catch (FinderException e) {
				e.printStackTrace();
			}
			req.setAttribute("student", student.getDescriptor());
			req.setAttribute("feedback", student.getAcademicSupFeedback().getDescriptor());
			//req.setAttribute("academicSup", student.getAcademicSup().getDescriptor());
			dispatcher = req.getRequestDispatcher("marker/markerViewReport.jsp") ;
		}if(page.equalsIgnoreCase("markerSettings")){
			req.setAttribute("result", "");
			req.setAttribute("marker", marker.getDescriptor());
			dispatcher = req.getRequestDispatcher("marker/markerSettings.jsp");
		}if(page.equalsIgnoreCase("setSettings")){
			String firstName = req.getParameter("firstName") ;
			String surName = req.getParameter("surName") ;
			String passwd = req.getParameter("passwd") ;
			String passwd2 = req.getParameter("passwd2") ;
			String email = req.getParameter("email") ;
			String result = "Changement done!";
			if(!passwd.equals("") || !passwd2.equals("")){
				//change the passwd;
				if(passwd.equals(passwd2)){
					marker.setPassword(Md5Encryption.getEncodedPassword(passwd));
					marker.setFirstname(firstName);
					marker.setSurname(surName);
					marker.setEmail(email);
				}else{
					result = "password don't match";
				}
			}else{
				marker.setFirstname(firstName);
				marker.setSurname(surName);
				marker.setEmail(email);
			}
			req.setAttribute("result", result);
			req.setAttribute("marker", marker.getDescriptor());
			dispatcher = req.getRequestDispatcher("marker/markerSettings.jsp");
		}
		if (dispatcher != null) dispatcher.forward(req, res) ;
	}
}
